home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Files / MoreIsBetter / MIB-Demos / MoreOpenAndSaveDemo / AnswerShell.c next >
Encoding:
C/C++ Source or Header  |  1998-09-25  |  5.6 KB  |  252 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        AnswerShell.c
  3.  
  4.     Contains:    shell for MoreOpenAndSaveDemo
  5.  
  6.     Written by:    Pete Gontier
  7.  
  8.     Copyright:    Copyright (c) 1997-1998 Apple Computer, Inc.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.          <4>     7/27/98    PCG     remove warnings re: missing params because Quinn treats warnings
  21.                                     as errors
  22.          <3>     7/24/98    PCG     remove #include "MoveableModalDialog.h"
  23.          <2>     7/11/98    PCG     add header
  24. */
  25.  
  26.  
  27. #define OLDROUTINELOCATIONS        0
  28. #define OLDROUTINENAMES            0
  29. #define SystemSevenOrLater        1
  30.  
  31. #include "MoreOpenAndSave.h"
  32. #include "MoreDialogs.h"
  33.  
  34. #include <Fonts.h>
  35. #include <Sound.h>
  36.  
  37. enum 
  38. {
  39.     kResID_Base = 128,
  40.     kResID_DialogItemList_CustomGetFile = kResID_Base,
  41.     kResID_DialogItemList_CustomPutFile
  42. };
  43.  
  44. static pascal OSErr InitMac (void)
  45. {
  46.     MaxApplZone ( );
  47.     InitGraf (&(qd.thePort));
  48.     InitFonts ( );
  49.     InitWindows ( );
  50.     InitMenus ( );
  51.     TEInit ( );
  52.     InitDialogs (nil);
  53.  
  54.     return noErr;
  55. }
  56.  
  57. static pascal Boolean CustomFileFilterProc (CInfoPBPtr cipbp, void *)
  58. {
  59.     Boolean dropIt = false;
  60.  
  61.     if (!(cipbp->hFileInfo.ioFlAttrib & ioDirMask))
  62.         if (cipbp->hFileInfo.ioFlFndrInfo.fdCreator != 'CWIE')
  63.             dropIt = true;
  64.  
  65.     return dropIt;
  66. }
  67.  
  68. static pascal Boolean StandardFileFilterProc (CInfoPBPtr cipbp)
  69. {
  70.     Boolean dropIt = false;
  71.  
  72.     //
  73.     //    File filters for StandardGetFile should never be asked
  74.     //    to filter directories. We test here for that problem.
  75.     //
  76.  
  77.     if (cipbp->hFileInfo.ioFlAttrib & ioDirMask)
  78.         DebugStr ("\pI was told there'd be no math.");
  79.     else if (cipbp->hFileInfo.ioFlFndrInfo.fdCreator != 'CWIE')
  80.         dropIt = true;
  81.  
  82.     return dropIt;
  83. }
  84.  
  85. static pascal Boolean ModalFilterYDProc
  86.     (DialogPtr /* dialog */, EventRecord * /* event */, short * /* itemHit */, void * /* yourDataPtr */)
  87. {
  88.     SysBeep (-1);
  89.     return false;
  90. }
  91.  
  92. static pascal OSErr MyStandardGetFile (void)
  93. {
  94.     OSErr err = noErr;
  95.  
  96.     FileFilterUPP upp = NewFileFilterProc (StandardFileFilterProc);
  97.  
  98.     if (!upp)
  99.         err = MemError ( );
  100.     else
  101.     {
  102.         StandardFileReply    sfr;
  103.         SFTypeList            sft        = { 'TEXT' };
  104.  
  105.         err = MOASI_StandardGetFile (upp,1,sft,&sfr);
  106.  
  107.         DisposeRoutineDescriptor (upp);
  108.  
  109.         if (!err && sfr.sfGood)
  110.         {
  111.             err = FSMakeFSSpec (sfr.sfFile.vRefNum, sfr.sfFile.parID, sfr.sfFile.name, &(sfr.sfFile));
  112.         }
  113.     }
  114.  
  115.     return err;
  116. }
  117.  
  118. static pascal OSErr MyStandardOpenDialog (void)
  119. {
  120.     StandardFileReply reply;
  121.  
  122.     return MOASI_StandardOpenDialog (&reply);
  123. }
  124.  
  125. static pascal OSErr MyStandardPutFile (void)
  126. {
  127.     StandardFileReply reply;
  128.  
  129.     return MOASI_StandardPutFile ("\pPrompt","\pDefaultFileName",&reply);
  130. }
  131.  
  132. static pascal OSErr MyCustomGetFile (void)
  133. {
  134.     OSErr err = noErr;
  135.  
  136.     FileFilterYDUPP fileFilterUPP = NewFileFilterYDProc (CustomFileFilterProc);
  137.  
  138.     if (!fileFilterUPP)
  139.         err = MemError ( );
  140.     else
  141.     {
  142.         ModalFilterYDUPP eventFilterUPP = NewModalFilterYDProc (ModalFilterYDProc);
  143.  
  144.         if (!eventFilterUPP)
  145.             err = MemError ( );
  146.         else
  147.         {
  148.             StandardFileReply    sfr;
  149.             SFTypeList            sft                = { 'TEXT' };
  150.             void                *context        = nil;
  151.         
  152.             err = MOASI_CustomGetFile (fileFilterUPP,1,sft,&sfr,kResID_DialogItemList_CustomGetFile,nil,eventFilterUPP,context);
  153.  
  154.             DisposeRoutineDescriptor (eventFilterUPP);
  155.         }
  156.  
  157.         DisposeRoutineDescriptor (fileFilterUPP);
  158.     }
  159.  
  160.     return err;
  161. }
  162.  
  163. static pascal OSErr MyCustomPutFile (void)
  164. {
  165.     OSErr err = noErr;
  166.  
  167.     StandardFileReply    reply;
  168.     ModalFilterYDUPP    filterProcUPP    = nil;
  169.     void                *context        = nil;
  170.  
  171.     err = MOASI_CustomPutFile ("\pPrompt","\pDefaultFileName",&reply,kResID_DialogItemList_CustomPutFile,nil,filterProcUPP,context);
  172.  
  173.     return err;
  174. }
  175.  
  176. static pascal Boolean ModalFilterProc (DialogRef dialog, EventRecord *event, short *itemHit)
  177. {
  178.     return StdFilterProc (dialog,event,itemHit);
  179. }
  180.  
  181. void main (void)
  182. {
  183.     if (InitMac ( ))
  184.         SysBeep (-1);
  185.     else
  186.     {
  187.         DialogRef dlgRef = GetNewDialog (129,nil,(WindowRef)-1);
  188.         if (dlgRef)
  189.         {
  190.             ModalFilterUPP modalFilterUPP = NewModalFilterProc (ModalFilterProc);
  191.             if (modalFilterUPP)
  192.             {
  193.                 enum
  194.                 {
  195.                     kDialogItemIndex_PushButton_Quit = kStdOkItemIndex,
  196.                     kDialogItemIndex_PushButton_StandardGetFile,
  197.                     kDialogItemIndex_PushButton_StandardPutFile,
  198.                     kDialogItemIndex_PushButton_StandardOpenFile,
  199.                     kDialogItemIndex_PushButton_CustomPutFile,
  200.                     kDialogItemIndex_PushButton_CustomGetFile
  201.                 };
  202.  
  203.                 short itemHit;
  204.  
  205.                 SetDialogDefaultItem (dlgRef,kDialogItemIndex_PushButton_Quit);
  206.                 SetDialogTracksCursor (dlgRef,true);
  207.  
  208.                 do
  209.                 {
  210.                     OSErr err = noErr;
  211.  
  212.                     MoveableModalDialog (modalFilterUPP,&itemHit);
  213.  
  214.                     switch (itemHit)
  215.                     {
  216.                         case kDialogItemIndex_PushButton_StandardGetFile :
  217.  
  218.                             err = MyStandardGetFile ( );
  219.                             break;
  220.  
  221.                         case kDialogItemIndex_PushButton_StandardPutFile :
  222.  
  223.                             err = MyStandardPutFile ( );
  224.                             break;
  225.  
  226.                         case kDialogItemIndex_PushButton_StandardOpenFile :
  227.  
  228.                             err = MyStandardOpenDialog ( );
  229.                             break;
  230.  
  231.                         case kDialogItemIndex_PushButton_CustomGetFile :
  232.  
  233.                             err = MyCustomGetFile ( );
  234.                             break;
  235.  
  236.                         case kDialogItemIndex_PushButton_CustomPutFile :
  237.  
  238.                             err = MyCustomPutFile ( );
  239.                             break;
  240.                     }
  241.  
  242.                     if (err) SysBeep (-1);
  243.                 }
  244.                 while (itemHit != kDialogItemIndex_PushButton_Quit);
  245.  
  246.                 DisposeRoutineDescriptor (modalFilterUPP);
  247.             }
  248.             DisposeDialog (dlgRef);
  249.         }
  250.     }
  251. }
  252.